package projects.myDvdCollection.pageObjects;

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;

import projects.myDvdCollection.pageObjects.elements.SearchPageElements;
import fw.ApplicationManager;

public class SearchPage extends SearchPageElements {

	public SearchPage(ApplicationManager app) {
		super(app);
	}

	public SearchPage resetSearchQerry() {
		setSearchQerry("");
		return this;
	}

	public SearchPage setSearchQerry(String filmName) {
		txtSearch().clear();
		txtSearch().sendKeys(filmName + Keys.ENTER);
		app.js().waitForAjax();
		return this;
	}

	public boolean isSearchCorrect(String filmName) {
		int i = 0;
		if (searchResult() != null) {
			int listSize = searchResult().size();
			if (listSize > 0) {
				for (WebElement film : searchResult()) {
					if (film.getText().trim().toLowerCase().contains(filmName.toLowerCase())) {
						i++;
					}
				}
				if (i == listSize) {
					return true;
				}
			}
		}
		return false;
	}

	public boolean isThereDifferentFilms() {
		int i = 0;
		String firstFilmName = "";
		if (searchResult() != null) {
			int listSize = searchResult().size();
			if (listSize > 1) {
				firstFilmName = searchResult().get(0).getText().trim().toLowerCase();
				for (WebElement film : searchResult()) {
					String currentFilmName = film.getText().trim().toLowerCase();
					if (!currentFilmName.equals(firstFilmName)) {
						i++;
					}
					if (i > 1) {
						return true;
					}
				}
			}
		}
		return false;
	}

}
